from django.db import models

from apps.classify.models import WorksCategory


class WorksList(models.Model):
    """作品集"""
    nickname = models.CharField(max_length=50, verbose_name='名称')
    category = models.ForeignKey(WorksCategory, on_delete=models.PROTECT, related_name='category', verbose_name='二级分类')
    category_v2 = models.ForeignKey(WorksCategory, on_delete=models.PROTECT, related_name='category_v2', verbose_name='一级分类')
    intro = models.CharField(max_length=50, verbose_name='简介')
    title = models.CharField(max_length=50, verbose_name='标题')
    image = models.CharField(max_length=200, verbose_name='图片', default='')
    tags = models.CharField(max_length=50, verbose_name='标签', null=True, default='')
    isFinished = models.BooleanField(default=False, verbose_name='是否完结')
    tracks = models.IntegerField(default=0, verbose_name='点击数')
    comments = models.IntegerField(default=0, verbose_name='评价数')
    draft = models.IntegerField(default=0, verbose_name='收藏数')
    isVip = models.BooleanField(default=False, verbose_name='是否要vip')
    status = models.BooleanField(default=True, verbose_name='状态')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_workslist'
        verbose_name = '作品集'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.nickname


class WorksListImage(models.Model):
    """作品集图片"""
    works = models.ForeignKey(WorksList, on_delete=models.CASCADE, verbose_name='works')
    small = models.CharField(max_length=200, verbose_name='小图片')
    middle = models.CharField(max_length=200, verbose_name='中图片')
    large = models.CharField(max_length=200, verbose_name='大图片')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_works_image'
        verbose_name = '作品集图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s %s' % (self.works.nickname, self.id)


class Work(models.Model):
    """作品"""
    title = models.CharField(max_length=50, verbose_name='名称')
    works = models.ForeignKey(WorksList, on_delete=models.CASCADE, verbose_name='作品集')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    stock = models.IntegerField(default=0, verbose_name='播放数')
    default_image = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')
    voice_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='音频文件地址')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_work'
        verbose_name = '作品work'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.title)
